package com.go.where.manager.action.go;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;

import com.go.where.manager.action.SuperAction;
import com.go.where.manager.common.Constants;
import com.go.where.manager.freemarker.CustomFreemarkerManager;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.BigClassInfo;
import com.go.where.manager.pojo.SmallClassInfo;
import com.go.where.manager.pojo.user.OperatorInfo;
import com.go.where.manager.service.BigClassService;
import com.go.where.manager.service.SmallClassService;
import com.go.where.manager.service.system.OperatorService;
import com.go.where.manager.util.CommonUtil;
import com.go.where.manager.util.KindEditUtil;

@InterceptorRefs( {
		@InterceptorRef(value = "defaultStack", params = { "maximumSize",
				"2048000", "allowedTypes",
				"image/bmp,image/png,image/gif,image/jpeg" }),
		@InterceptorRef(value = "defalutStackSet") })
public class SmallClassAction extends SuperAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4383195694100314090L;
	private static final Logger log = Logger.getLogger(SmallClassAction.class);

	public Pagination<?> page;

	@Resource(name = "bigClassService")
	private BigClassService bigClassService;
	@Resource(name = "smallClassService")
	private SmallClassService smallClassService;
	@Resource
	private OperatorService operatorService;

	private SmallClassInfo smallClassInfo;
	private SmallClassInfo smallClassInfoQuery;

	private List<BigClassInfo> bigClassList;

	private String classId;
	private String top;
	private String hot;
	private String subClassId;

	private File icon;// 上传的文件
	private String iconFileName; // 文件名称
	private String iconContentType; // 文件类型

	/**
	 * 查询列表
	 * 
	 * @return
	 */
	public String list() {
		if (smallClassInfoQuery == null) {
			smallClassInfoQuery = new SmallClassInfo();
			smallClassInfoQuery.setPageSize(Constants.INIT_PAGESIZE);
			smallClassInfoQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			String userId = getCurrentOperator().getUserId();
			OperatorInfo op = operatorService.queryOperatorInfoByUserId(userId);
			if (op != null) {
				String roleId = op.getRoleId();
				if (CustomFreemarkerManager.getValue("system.merchant.role.id")
						.equals(roleId)) {
					smallClassInfoQuery.setMroleId(roleId);
				}
			}
			smallClassInfoQuery.setIsDelete(Constants.NOT_DELETED);
			page = smallClassService.queryPage(smallClassInfoQuery);
		} catch (Exception e) {
			addActionError(e.getMessage());
			log.error("查询信息异常", e);
		}
		return LIST;
	}

	public String toEdit() {
		smallClassInfo = smallClassService.queryByPrimartKey(classId);
		OperatorInfo op = operatorService
				.queryOperatorInfoByUserId(getCurrentOperator().getUserId());
		String mroleId = null;
		if (op != null) {
			String roleId = op.getRoleId();
			if (CustomFreemarkerManager.getValue("system.merchant.role.id")
					.equals(roleId)) {
				mroleId = roleId;
			}
		}
		BigClassInfo bi = new BigClassInfo();
		bi.setMroleId(mroleId);
		bigClassList = bigClassService.queryAll(bi);
		return INPUT;
	}

	public String add() {
		subClassId = smallClassService.getSubKey();
		OperatorInfo op = operatorService
				.queryOperatorInfoByUserId(getCurrentOperator().getUserId());
		String mroleId = null;
		if (op != null) {
			String roleId = op.getRoleId();
			if (CustomFreemarkerManager.getValue("system.merchant.role.id")
					.equals(roleId)) {
				mroleId = roleId;
			}
		}
		BigClassInfo bi = new BigClassInfo();
		bi.setMroleId(mroleId);
		bigClassList = bigClassService.queryAll(bi);
		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			String iconRealpath = ServletActionContext.getServletContext()
					.getRealPath(
							CommonUtil.FILE_SEPARATOR
									+ Constants.SMALL_CLASS_ICON_IMAGES);
			String iconPath = KindEditUtil.uploadPicture(icon, iconFileName,
					iconRealpath, Constants.SMALL_CLASS_ICON_IMAGES);
			smallClassInfo.setIconPath(iconPath);
			if (smallClassInfo != null) {
				if (StringUtils.isBlank(smallClassInfo.getIsHot())) {
					smallClassInfo.setIsHot("0");
				}
				if (StringUtils.isBlank(smallClassInfo.getIsTop())) {
					smallClassInfo.setIsTop("0");
				}
			}
			smallClassService.save(smallClassInfo, getCurrentOperator());
			redirectionUrl = "small_class!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("保存信息异常", e);
			addActionError("保存信息异常！" + e);
			return this.add();
		}
	}

	/**
	 * 编辑
	 * 
	 * @return
	 */
	public String edit() {
		try {
			if (icon != null) {
				String iconRealpath = ServletActionContext.getServletContext()
						.getRealPath(
								CommonUtil.FILE_SEPARATOR
										+ Constants.SMALL_CLASS_ICON_IMAGES);
				String iconPath = KindEditUtil.uploadPicture(icon,
						iconFileName, iconRealpath,
						Constants.SMALL_CLASS_ICON_IMAGES);
				smallClassInfo.setIconPath(iconPath);
			}
			if (smallClassInfo != null) {
				if (StringUtils.isBlank(smallClassInfo.getIsHot())) {
					smallClassInfo.setIsHot("0");
				}
				if (StringUtils.isBlank(smallClassInfo.getIsTop())) {
					smallClassInfo.setIsTop("0");
				}
			}
			smallClassService.update(smallClassInfo, getCurrentOperator());
			redirectionUrl = "small_class!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("修改信息异常", e);
			addActionError("修改信息异常！" + e);
			classId = smallClassInfo.getClassId();
			return this.toEdit();
		}
	}

	/**
	 * 详情
	 * 
	 * @return
	 */
	public String detail() {
		smallClassInfo = smallClassService.queryByPrimartKey(classId);
		OperatorInfo op = operatorService
				.queryOperatorInfoByUserId(getCurrentOperator().getUserId());
		String mroleId = null;
		if (op != null) {
			String roleId = op.getRoleId();
			if (CustomFreemarkerManager.getValue("system.merchant.role.id")
					.equals(roleId)) {
				mroleId = roleId;
			}
		}
		BigClassInfo bi = new BigClassInfo();
		bi.setMroleId(mroleId);
		bigClassList = bigClassService.queryAll(bi);
		return "detail";
	}

	/**
	 * 设置置顶或取消
	 * 
	 * @return
	 */
	public String setTop() {
		smallClassInfo = new SmallClassInfo();
		smallClassInfo.setClassId(classId);
		smallClassInfo.setIsTop(top);
		try {
			smallClassService.update(smallClassInfo, getCurrentOperator());
			redirectionUrl = "small_class!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("置顶信息设置异常", e);
			addActionError("置顶信息设置异常" + e);
			return this.list();
		}
	}

	/**
	 * 设置热门或取消
	 * 
	 * @return
	 */
	public String setHot() {
		smallClassInfo = new SmallClassInfo();
		smallClassInfo.setClassId(classId);
		smallClassInfo.setIsHot(hot);
		try {
			smallClassService.update(smallClassInfo, getCurrentOperator());
			redirectionUrl = "small_class!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("热门信息设置异常", e);
			addActionError("热门信息设置异常" + e);
			return this.list();
		}
	}

	public String falseDelete() {
		smallClassInfo = new SmallClassInfo();
		smallClassInfo.setClassId(classId);
		smallClassInfo.setIsDelete(Constants.DELETED);
		try {
			smallClassService.update(smallClassInfo, getCurrentOperator());
			redirectionUrl = "small_class!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("删除信息异常", e);
			addActionError("删除信息异常" + e);
			return this.list();
		}
	}

	public String getClassId() {
		return classId;
	}

	public void setClassId(String classId) {
		this.classId = classId;
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public String getTop() {
		return top;
	}

	public void setTop(String top) {
		this.top = top;
	}

	public String getHot() {
		return hot;
	}

	public void setHot(String hot) {
		this.hot = hot;
	}

	public SmallClassInfo getSmallClassInfo() {
		return smallClassInfo;
	}

	public void setSmallClassInfo(SmallClassInfo smallClassInfo) {
		this.smallClassInfo = smallClassInfo;
	}

	public SmallClassInfo getSmallClassInfoQuery() {
		return smallClassInfoQuery;
	}

	public void setSmallClassInfoQuery(SmallClassInfo smallClassInfoQuery) {
		this.smallClassInfoQuery = smallClassInfoQuery;
	}

	public String getSubClassId() {
		return subClassId;
	}

	public void setSubClassId(String subClassId) {
		this.subClassId = subClassId;
	}

	public List<BigClassInfo> getBigClassList() {
		return bigClassList;
	}

	public void setBigClassList(List<BigClassInfo> bigClassList) {
		this.bigClassList = bigClassList;
	}

	public File getIcon() {
		return icon;
	}

	public void setIcon(File icon) {
		this.icon = icon;
	}

	public String getIconFileName() {
		return iconFileName;
	}

	public void setIconFileName(String iconFileName) {
		this.iconFileName = iconFileName;
	}

	public String getIconContentType() {
		return iconContentType;
	}

	public void setIconContentType(String iconContentType) {
		this.iconContentType = iconContentType;
	}

}
